class SingersController < ApplicationController
  skip_before_action :authenticate_request, only: [:show, :index]
  
  def create
    document = JSONAPI.parse(params.to_unsafe_h)
    @singer = Singer.new(name: document.data.attributes.name, height: document.data.attributes.height, sex: document.data.attributes.sex, pic: document.data.attributes.pic)
    @singer.save!
    obj_return(@singer, status: 201)
  end

  def index
    obj_return(Singer.all, status: 200)
  end

  def update
    document = JSONAPI.parse(params.to_unsafe_h)
    @singer = Singer.find(params[:id])
    @singer.name = document.data.attributes.name if document.data.attributes.defined?(:name)
    @singer.height = document.data.attributes.height if document.data.attributes.defined?(:height)
    @singer.sex = document.data.attributes.sex if document.data.attributes.defined?(:sex)
    @singer.pic = document.data.attributes.pic if document.data.attributes.defined?(:pic)
    @singer.save
    obj_return(@singer, status:200)
  end

  def show
    @singer = Singer.find(params[:id])
    obj_return(@singer, status: 200)
  end

  def destroy
    @singer = Singer.find(params[:id])
    @singer.musics.destroy_all
    @singer.destroy
    err_return(:ok, status: 200)
  end
end
